/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.gob.pnp.despachadores;

import java.util.*;
import pe.gob.pnp.actions.*;
import java.sql.*;
import oracle.jdbc.OracleTypes;
import pe.gob.pnp.factories.ConnectionPool;

/**
 *
 * @author Administrador
 */
public class ConceptoDespachador {

    public static List<Concepto> obtenerConcepto() throws SQLException {
        List<Concepto> lstConcepto = new ArrayList<Concepto>();
        Connection conn = null;
        try {
            conn = ConnectionPool.obtenerConexion();
            String sql = "{call SP_OBTENER_CONCEPTO(?)}";
            CallableStatement cs = conn.prepareCall(sql);
            cs.registerOutParameter(1, OracleTypes.CURSOR);
            cs.execute();
            ResultSet rs = (ResultSet) cs.getObject(1);
            while (rs.next()) {
                Concepto concepto = new Concepto();
                concepto.setNconcIdentificador(rs.getInt("NCONC_IDENTIFICADOR"));
                concepto.setCconcDescripcion(rs.getString("CCONC_DESCRIPCION"));
                concepto.setBconcActivo(rs.getBoolean("BCONC_ACTIVO"));
                lstConcepto.add(concepto);
            }
        } finally {
            ConnectionPool.liberarConexion(conn);
        }
        return lstConcepto;
    }

    public static void agregarConcepto(Concepto concepto) throws SQLException {
        Connection conn = null;
        try {
            conn = ConnectionPool.obtenerConexion();
            String sql = "{call SP_AGREGAR_CONCEPTO(?,?)}";
            CallableStatement cs = conn.prepareCall(sql);
            cs.setString(1, concepto.getCconcDescripcion());
            cs.setInt(2, concepto.isBconcActivo() ? 1 : 0);
            cs.execute();
        } finally {
            ConnectionPool.liberarConexion(conn);
        }
    }

    public static void modificarConcepto(Concepto concepto) throws SQLException {
        Connection conn = null;
        try {
            conn = ConnectionPool.obtenerConexion();
            String sql = "{call SP_MODIFICAR_CONCEPTO(?,?,?)}";
            CallableStatement cs = conn.prepareCall(sql);
            cs.setString(1, concepto.getCconcDescripcion());
            cs.setInt(2, concepto.isBconcActivo() ? 1 : 0);
            cs.setInt(3, concepto.getNconcIdentificador());
            cs.execute();
        } finally {
            ConnectionPool.liberarConexion(conn);
        }
    }
}
